package com.ikas.autumn.iec104.common.util;


/**
 * 随机生成遥信遥测数据
 * @author dbh
 */
public class ChangeUtils {
    private static final char[] HEX_CHAR_TABLE = {
            '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
    };

    public static String toHexString(Byte[] data){
        byte[] resultBytes = new byte[data.length];
        for(int i =0 ;i<data.length;i++){
            resultBytes[i] = data[i];
        }
        return toHexString(resultBytes);
    }

    public static String toHexString(byte[] data){
        if(data == null || data.length == 0)
            return null;
        byte[] hex = new byte[data.length * 2];
        int index = 0;
        for(byte b : data){
            int v = b & 0xFF;
            hex[index++] = (byte) HEX_CHAR_TABLE[v >>> 4];
            hex[index++] = (byte) HEX_CHAR_TABLE[v & 0xF];
        }
        return new String(hex);
    }

    public static byte[] hexStringToBytes(String data){
        if(data == null || "".equals(data)) {
            return null;
        }
        data = data.toUpperCase();
        int length = data.length()/2;
        char[] dataChars = data.toCharArray();
        byte[] byteData = new byte[length];
        for (int i = 0;i<length;i++){
            int pos = i * 2;
            byteData[i] = (byte)(charToByte(dataChars[pos]) << 4 | charToByte(dataChars[pos + 1]) & 0xff);
        }
        return byteData;
    }

    public static byte charToByte(char c){
        return (byte)"0123456789ABCDEF".indexOf(c);
    }

    public static String byteAppend(byte[] bytes){
        StringBuilder str = new StringBuilder();
        for (int i=bytes.length-1;i>=0;i--){
            str.append(String.format("%02d",bytes[i]));
        }
        return str.toString();
    }

    public static String floatToHexstr(float value){
        byte[] buffer = new byte[4];
        new IeShortFloat(value).encode(buffer,0);
        return toHexString(buffer);
    }

    public static String encode(float value){
        int tempVal = Float.floatToIntBits(value);
        byte[] buffer = new byte[4];
        buffer[0] = (byte) tempVal;
        buffer[1] = (byte) (tempVal >> 8);
        buffer[2] = (byte) (tempVal >> 16);
        buffer[3] = (byte) (tempVal >> 24);
        int[] s = new int[4];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < buffer.length; i++){
            s[i] = (buffer[i] & 0xff);
            if (Integer.toHexString(s[i]).length() == 1){
                sb.append("0").append(Integer.toHexString(s[i])).append(" ");
            }else {
                sb.append(Integer.toHexString(s[i])).append(" ");
            }
        }
        return sb.toString().toUpperCase();
    }

    public static String encodeInfomationAddress(int address) {
        byte[] buffer = new byte[3];
        buffer[0] = (byte) address;
        buffer[1] = (byte) (address >> 8);
        buffer[2] = (byte) (address >> 16);

        int[] s = new int[4];
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < buffer.length; i++){
            s[i] = (buffer[i] & 0xff);
            if (Integer.toHexString(s[i]).length() == 1){
                sb.append("0").append(Integer.toHexString(s[i])).append(" ");
            }else {
                sb.append(Integer.toHexString(s[i])).append(" ");
            }
        }
        return sb.toString().toUpperCase();
    }

}